package com.threeti.mecool.web.assembler;

import com.threeti.mecool.core.domain.model.plan.UserStepResult;
import com.threeti.mecool.core.domain.model.plan.pojo.TaskActionDataType;
import com.threeti.mecool.core.infrastructure.persistence.plan.UserStepResultSpecs;
import com.threeti.mecool.framework.util.Lists;
import com.threeti.mecool.web.form.ChannelInfoForm;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;

import java.util.List;

public abstract class UserStepResultAssembler {

    public static Specification<UserStepResult> toCriteria(ChannelInfoForm ciForm, Pageable pageable) {
        Specifications<UserStepResult> spec = null;

        if (StringUtils.isNotBlank(ciForm.getChangeTypeSlt())) {
            List<TaskActionDataType> tadts;
            if ("0".equals(ciForm.getChangeTypeSlt())) {
                tadts = Lists.newArrayList(TaskActionDataType.findByCode(TaskActionDataType.CODE_INPUT_STORE_ADDRESS));
            } else {
                tadts = TaskActionDataType.findByCodes(TaskActionDataType.CODE_INPUT_CONTACT_NAME,
                        TaskActionDataType.CODE_INPUT_CONTACT_POSITION, TaskActionDataType.CODE_INPUT_CONTACT_WAY);
            }
            List<Long> partHintIds = Lists.newArrayList();
            for (TaskActionDataType tadt : tadts) {
                partHintIds.add(tadt.getId());
            }
            spec = Specifications.where(UserStepResultSpecs.dataTypeIdIn(partHintIds));
        } else {
            List<TaskActionDataType> hintAll = TaskActionDataType.findByCodes(TaskActionDataType.CODE_INPUT_STORE_ADDRESS, TaskActionDataType.CODE_INPUT_CONTACT_NAME,
                    TaskActionDataType.CODE_INPUT_CONTACT_POSITION, TaskActionDataType.CODE_INPUT_CONTACT_WAY);
            List<Long> hintAllIds = Lists.newArrayList();
            for (TaskActionDataType tadt : hintAll) {
                hintAllIds.add(tadt.getId());
            }
            spec = Specifications.where(UserStepResultSpecs.dataTypeIdIn(hintAllIds));
        }

        if (StringUtils.isNotBlank(ciForm.getFinishedAtFrom())) {
            spec = spec.and(UserStepResultSpecs.finishedAtGe(DateTime.parse(ciForm.getFinishedAtFrom()).toDate()));
        }

        if (StringUtils.isNotBlank(ciForm.getFinishedAtTo())) {
            spec = spec.and(UserStepResultSpecs.finishedAtLe(DateTime.parse(ciForm.getFinishedAtTo()).toDate()));
        }

        if (StringUtils.isNotBlank(ciForm.getEmpName())) {
            spec = spec.and(UserStepResultSpecs.empNameLike(ciForm.getEmpName()));
        }

        if (StringUtils.isNotBlank(ciForm.getStoreChannelName())) {
            spec = spec.and(UserStepResultSpecs.storeChannelNameLike(ciForm.getStoreChannelName()));
        }

        if (StringUtils.isNotBlank(ciForm.getProjectName())) {
            spec = spec.and(UserStepResultSpecs.projectNameLike(ciForm.getProjectName()));
        }

        if (StringUtils.isNotBlank(ciForm.getProjectCode())) {
            spec = spec.and(UserStepResultSpecs.projectCodeIs(ciForm.getProjectCode()));
        }

        if (StringUtils.isNotBlank(ciForm.getProjectCategoryIdSlt())) {
            Page<UserStepResult> usrs = UserStepResult.findByProjectCategoryId(Long.valueOf(ciForm.getProjectCategoryIdSlt()), pageable);
            List<String> projCodes = Lists.newArrayList();
            for (UserStepResult usr : usrs) {
                projCodes.add(usr.getProjectCode());
            }
            if (projCodes.size() != 0)
                spec = spec.and(UserStepResultSpecs.projectCodeIn(projCodes));
            else
                spec = spec.and(UserStepResultSpecs.projectCodeIs("-1"));

        }

        return spec;
    }
}
